import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 87111
 * Date: 2025-09-13
 * Time: 18:25
 */
public class Sort {
    /**
     *冒泡排序
     * 时间复杂度：O(n^2)
     * 空间复杂度：O(1)
     * 稳定性：稳定
     */
    public void bubbleSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length - 1 - i; j++) {
                if(array[j] > array[j+1]) {
                    swap(array,j,j+1);
                }
            }
        }
    }
    private void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    /**
     * 快速排序(Hoare版)
     * 时间复杂度：最坏的情况(顺序和逆序)O(n^2) 最好的情况O(nlogn)
     * 空间复杂度：最坏情况O(n)   最好情况O(logn)
     * 稳定性：不稳定
     */
    public void quickSort(int[] array) {
        quick(array,0,array.length-1);
    }
    private void quick(int[] array, int left, int right) {
        if(left >= right) {
            return;
        }

//        //三数取中法
//        int index = findCenter(array,left,right);
//        swap(array,left,index);

//        //直接插入排序
//        if(right - left + 1 <= 20) {
//            insertSort(array,left,right);
//            return;
//        }

        int pivot = partition(array, left, right);
        quick(array,left,pivot-1);
        quick(array,pivot+1,right);

    }

    //当数据大小小于等于20时采用插入排序
    private void insertSort(int[] array, int left, int right) {
        for (int i = left+1; i <= right; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j >= left; j--) {
                if(array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    private int findCenter(int[] array, int left, int right) {
        int tmp = (left + right) / 2;
        if(array[left] < array[right]) {
            if(array[left] > array[tmp]) {
                return left;
            }else if(array[tmp] > array[right]) {
                return right;
            }else {
                return tmp;
            }
        }else {
            if(array[left] < array[tmp]) {
                return left;
            }else if(array[tmp] < array[right]) {
                return right;
            }else {
                return tmp;
            }
        }
    }

    //该方法在每次结束后返回left和right相遇的地方。
    //Hoare法
    private int partition1(int[] array, int left, int right) {
        int first = left;
        int tmp = array[left];
        while(left < right) {
            while(left < right && array[right] >= tmp) {
                right--;
            }
            while(left < right && array[left] <= tmp) {
                left++;
            }
            swap(array,left,right);
        }
        swap(array,first,left);
        return left;
    }

    //挖坑法
    private int partition(int[] array, int left, int right) {
        int tmp = array[left];
        while(left < right) {
            while(left < right && array[right] >= tmp) {
                right--;
            }
            array[left] = array[right];
            while(left < right && array[left] <= tmp) {
                left++;
            }
            array[right] = array[left];
        }
        array[left] = tmp;
        return left;
    }

    //前后指针法
    private int partition3(int[] array, int left, int right) {
        int prev = left;
        int cur = left + 1;
        while(cur <= right) {
            if(array[cur] < array[left] && array[++prev] != array[cur]) {
                swap(array,prev,cur);
            }
            cur++;
        }
        swap(array,prev,left);
        return prev;
    }

    //非递归实现快速排序
    public void quickSortNonR(int[] array) {
        int left = 0;
        int right = array.length - 1;
        Stack<Integer> stack = new Stack<>();
        //调用挖空法快速排序
        int pivot = partition(array,left,right);
        if(pivot > left + 1) {
            stack.push(left);
            stack.push(pivot - 1);
        }
        if(pivot < right - 1) {
            stack.push(pivot+1);
            stack.push(right);
        }
        while(!stack.isEmpty()) {
            right = stack.pop();
            left = stack.pop();
            pivot = partition(array,left,right);
            if(pivot > left + 1) {
                stack.push(left);
                stack.push(pivot - 1);
            }
            if(pivot < right - 1) {
                stack.push(pivot+1);
                stack.push(right);
            }
        }
    }

}
